#!/usr/bin/env php
<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

require_once 'app/init.php';
require_once APPPATH.'/../tests/Test.php';
require_once APPPATH.'/../tests/AirHttpTest.php';

// init
AIR2_DBManager::init();
$browser = new AirHttpTest();
$browser->set_content_type(AirHttpTest::$JSON);
$browser->set_test_user();
$csv_dir = dirname(__FILE__).'/csv';

// helper function for POSTing a file
function upload_file($url, $fld_name, $file) {
    global $browser;

    $session = curl_init($browser->base_url.$url);
    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($session, CURLOPT_POST, true);
    curl_setopt($session, CURLOPT_POSTFIELDS, array($fld_name => "@$file"));
    foreach ($browser->cookies as $nm => $ck) {
        curl_setopt($session, CURLOPT_COOKIE, "$nm=$ck");
    }
    curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($session, CURLOPT_SSL_VERIFYHOST, false);

    return curl_exec($session);
}


plan(41);

/**********************
 * Upload a valid csv file
 */
$resp = upload_file('upload.json', 'csvfile', "$csv_dir/test_header.csv");
ok( $resp, 'upload valid' );
ok( $json = json_decode($resp, true), 'upload valid - decode' );
ok( isset($json['success']) && $json['success'], 'upload valid - success' );
$uuid = $json['radix']['tank_uuid'];

/**********************
 * Submit non-file (fails w/404)
 */
ok( $resp = $browser->http_post("upload/DNE12345/submit"), 'non-existent' );
is( $browser->resp_code(), 404, 'non-existent - 404' );

/**********************
 * Submit the file with wrong mapping (fails)
 */
ok( $resp = $browser->http_post("upload/$uuid/submit?nojob=1", array('d' => ';')), 'bad mapping' );
is( $browser->resp_code(), 200, 'bad mapping - 200' );
ok( $json = json_decode($resp, true), 'bad mapping - decode' );
ok( !$json['success'], 'bad mapping - unsuccess' );
$msg1 = $json['message'];

/**********************
 * Submit the file with good mapping
 */
ok( $resp = $browser->http_post("upload/$uuid/submit?nojob=1", array('d' => ',')), 'no mapping' );
is( $browser->resp_code(), 200, 'no mapping - 200' );
ok( $json = json_decode($resp, true), 'no mapping - decode' );
ok( $json['success'], 'no mapping - success' );
isnt( $json['message'], $msg1, 'no mapping - new message' );

// check record status, and tank_sources
$rec = Doctrine::getTable('Tank')->findOneBy('tank_uuid', $uuid);
is( $rec->tank_status, Tank::$STATUS_IMPORTED, 'no mapping - tank_status' );
$srcs = $rec->TankSource;
is( count($srcs), 6, 'no mapping - TankSource count' );
foreach ($srcs as $idx => $tsrc) {
    is( $tsrc->tsrc_status, TankSource::$STATUS_NEW, "no mapping - tsrc_status $idx" );
}


/**********************
 * Resubmit a completed file (fails)
 */
is( $rec->tank_status, Tank::$STATUS_IMPORTED, 'resubmit - tank status starts as (S)uccessful' );
ok( $resp = $browser->http_post("upload/$uuid/submit?nojob=1"), 'resubmit 1' );
is( $browser->resp_code(), 200, 'resubmit 1 - 200' );
ok( $json = json_decode($resp, true), 'resubmit 1 - decode' );
ok( !$json['success'], 'resubmit 1 - unsuccess' );

// change status to (F)ailed
$rec->tank_status = Tank::$STATUS_IMPORT_FAILED;
$rec->save();
ok( $resp = $browser->http_post("upload/$uuid/submit?nojob=1"), 'resubmit 2' );
is( $browser->resp_code(), 200, 'resubmit 2 - 200' );
ok( $json = json_decode($resp, true), 'resubmit 2 - decode' );
ok( $json['success'], 'resubmit 2 - success' );
$rec->refresh(true);
is( $rec->tank_status, Tank::$STATUS_IMPORTED, 'resubmit 2 - tank status ends as (S)uccessful' );
is( count($rec->TankSource), 12, 'resubmit 2 - TankSource count' );
$rec->delete();


/**********************
 * Trigger a doctrine-validation error with the mapping
 */
$resp = upload_file('upload.json', 'csvfile', "$csv_dir/test_header_error.csv");
ok( $resp, 'upload2 valid' );
ok( $json = json_decode($resp, true), 'upload2 valid - decode' );
ok( isset($json['success']) && $json['success'], 'upload2 valid - success' );
$uuid = $json['radix']['tank_uuid'];
$rec = Doctrine::getTable('Tank')->findOneBy('tank_uuid', $uuid);

ok( $resp = $browser->http_post("upload/$uuid/submit?nojob=1"), 'bad type mapping' );
is( $browser->resp_code(), 200, 'bad type mapping - 200' );
ok( $json = json_decode($resp, true), 'bad type mapping - decode' );
ok( !$json['success'], 'bad type mapping - unsuccess' );
ok( preg_match('/smadd_zip/', $json['message']), 'bad type mapping - problem with smadd_zip' );

// cleanup
$rec->delete();

?>
